/*
 * @(#)RadioTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.taglib.html;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.util.ResponseUtils;

import com.dcivision.framework.Utility;

/**
  RadioTag.java

  <p>This class is to to addin javascript checing during input radio onchange. </p>
  <p>Purpose to Override struts taglib for tracing form modification.<p>
  <p>Modification in "struts-html.tld"<br>
  <pre>
     [FROM]
     &lt;tagclass&gt;org.apache.struts.taglib.html.RadioTag&lt;/tagclass&gt;
     [TO]
     &lt;tagclass&gt;com.dcivision.framework.taglib.html.RadioTag&lt;/tagclass&gt;

     [ADDED]
      &lt;attribute&gt;
      &lt;name&gt;textOnly&lt;/name&gt;
      &lt;required&gt;false&lt;/required&gt;
      &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
      &lt;/attribute&gt;

  <p>USAGE:</p>
  <p>The following example shows the usage of RadioTag  </p>
  <p><code>&lt;html:radio <strong>property='updateDate' textOnly='false'</strong>&gt;display value&lt;/html:radio&gt;</code></p>

  </pre>
  </p>

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   09/07/2003
    @version         $Revision: 1.11.32.2 $
*/

public class RadioTag extends org.apache.struts.taglib.html.RadioTag {

  public static final String REVISION = "$Revision: 1.11.32.2 $";
  
  private static final Log log = LogFactory.getLog(RadioTag.class);
  
  protected boolean textOnly = false;

  public int doStartTag() throws JspException {
    FormTag formTag = (FormTag)BodyTagSupport.findAncestorWithClass(this, FormTag.class);

    // if not editable
    if (this.getTextOnly() || (!Utility.isEmpty(formTag)&& formTag.getTextOnly())) {
      String sOutVal = "";
      boolean bMatch = false;

      // lookup the value stored in the bean
      String beanValue = this.lookupProperty(this.name, this.property);
      if (beanValue!= null) {
        if (beanValue.equals(value)) {
          sOutVal = ResponseUtils.filter(value);
          bMatch = true;
        }
      }
      // if matched with the selected one, print the hidden value and eval the body content; else skip the body
      if (bMatch) {
        try {
          JspWriter out = pageContext.getOut();
          out.println("<input type='hidden' name='" + this.property + "' value='" + sOutVal + "'>");
        } catch (Exception e) {
          log.error(e, e);
        }
        return(EVAL_BODY_INCLUDE);
      } else {
        return(SKIP_BODY);
      }
    }

    switch (BaseHandler.commonRoutine(this, pageContext, this.getTextOnly())) {
      case BaseHandler.DIRECT_SKIP_BODY:
        return SKIP_BODY;
      case BaseHandler.DIRECT_EVAL_BODY_BUFFERED:
        return EVAL_BODY_BUFFERED;
      case BaseHandler.RETURN_SUPER_DOSTART:
        return super.doStartTag();
      default:
        return super.doStartTag();
    }
  }

  public int doEndTag() throws JspException {
    FormTag formTag = (FormTag)BodyTagSupport.findAncestorWithClass(this, FormTag.class);
    if (this.getTextOnly() || (!Utility.isEmpty(formTag)&& formTag.getTextOnly())) {
      return(EVAL_PAGE);
    } else {
      return(super.doEndTag());
    }
  }

  public void setTextOnly(boolean val) {
    textOnly = val;
  }

  public boolean getTextOnly(){
    return textOnly;
  }

  public void release() {
    super.release();
    textOnly = false;
  }

}
